//-----------------------------------------------------------------------
// <copyright file="Load.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary>Loads the version number properties from the version number file specified.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.VersionNumber
{
    using System;
    using System.IO;
    using System.Xml;
    using System.Globalization;

    using Microsoft.Build.Framework;

    /// <summary>
    /// Loads the version number properties from the version number file specified.
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[
    /// <VersionNumber.Load
    ///             VersionNamePath="versionNamePath"
    ///             ConfigFileLocation="configFileLocation"
    ///             ConfigVSSLocation="configVSSLocation"
    ///             <Output TaskParameter="RevisionNumber" ItemName="revisionNumberItem"></Output>
    ///             <Output TaskParameter="MajorNumber" ItemName="majorNumberItem"></Output>
    ///             <Output TaskParameter="MinorNumber" ItemName="minorNumberItem"></Output>
    ///             <Output TaskParameter="BuildNumber" ItemName="buildNumberItem"></Output>
    ///             <Output TaskParameter="StartDate" ItemName="startDateItem"></Output>
    ///             <Output TaskParameter="VersionNumber" ItemName="versionNumberItem"></Output>
    ///             <Output TaskParameter="VersionName" ItemName="versionNameItem"></Output>
    /// </VersionNumber.Load>
    /// ]]></code>
    /// <para>where:</para>
    /// <para><i>versionNamePath</i></para>
    /// <para>The path to the root folder for the current version.</para>
    /// <para><i>configFileLocation</i></para>
    /// <para>Fully qualified file name to the xml config file. The file contains, in Xml format, the major, minor, build and revision number.</para>
    ///
    /// For example:
    /// 
    /// <example>
    /// <code><![CDATA[
    /// <?xml version="1.0" encoding="utf-8"?>
    ///     <configuration>
    ///         <appSettings>
    ///             <add key="major" value="1" />
    ///             <add key="minor" value="2" />
    ///             <add key="build" value="1354" />
    ///             <add key="revision" value="155" />
    ///         </appSettings>
    ///     </configuration>
    /// ]]></code>    
    /// </example>
    ///
    /// <para><i>configVSSLocation</i></para>
    /// <para>The path to the config file specified in <i>versionNumberConfigFileLocation</i> inside VSS, using VSS-style notation.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <VersionNumber.Load>
    ///               VersionNamePath="$(MSBuildProjectDirectory)\..\.."
    ///               ConfigFileLocation="@(VersionXmlFile)" 
    ///               <Output TaskParameter="VersionName" ItemName="VersionName">
    ///         </VersionNumber.Load> 
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class Load : TaskBase
    {
        private string versionNumberConfigFileLocation;
        private string versionNumberConfigVSSLocation;
        private string versionNamePath = String.Empty;
        private string revisionBuildNumber;
        private int majorBuildNumber;
        private int minorBuildNumber;
        private string buildBuildNumber;
        private DateTime startDate;
        private string buildNumberFull;
        private string versionName;
        
        private XmlDocument versionConfig = new XmlDocument();

        /// <summary>
        /// Initializes a new instance of the LoadVersionNumber class.
        /// </summary>
        public Load()
        {
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            #region Execute code

            if (versionNamePath.Length > 0)
            {
                this.versionName = Path.GetFileName(Path.GetFullPath(versionNamePath));
            }
            else
            {
                DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
                this.versionName = di.Parent.Name;
            }

            this.LoadFile();
            #endregion
        }

        private void LoadFile()
        {
            this.versionConfig.Load(this.versionNumberConfigFileLocation);

            XmlNode majorNode = this.versionConfig.SelectSingleNode("configuration/appSettings/add[@key='major']");
            if (majorNode != null)
            {
                this.majorBuildNumber = XmlConvert.ToInt32(majorNode.Attributes["value"].Value);
            }

            XmlNode minorNode = this.versionConfig.SelectSingleNode("configuration/appSettings/add[@key='minor']");
            if (minorNode != null)
            {
                this.minorBuildNumber = XmlConvert.ToInt32(minorNode.Attributes["value"].Value);
            }

            XmlNode buildNode = this.versionConfig.SelectSingleNode("configuration/appSettings/add[@key='build']");
            if (buildNode != null)
            {
                this.buildBuildNumber = buildNode.Attributes["value"].Value;
            }

            XmlNode revisionNode = this.versionConfig.SelectSingleNode("configuration/appSettings/add[@key='revision']");
            if (revisionNode != null)
            {
                this.revisionBuildNumber = revisionNode.Attributes["value"].Value;
            }

            XmlNode startDateNode = this.versionConfig.SelectSingleNode("configuration/appSettings/add[@key='startDate']");
            if (startDateNode != null)
            {
                this.startDate = XmlConvert.ToDateTime(startDateNode.Attributes["value"].Value,"yyyy-MM-dd");
            }

            buildNumberFull = String.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}.{3}", majorBuildNumber, minorBuildNumber, buildBuildNumber, this.revisionBuildNumber);
        }

        public string ConfigFileLocation
        {
            get
            {
                return (versionNumberConfigFileLocation == null ? String.Empty : versionNumberConfigFileLocation);
            }
            set
            {
                versionNumberConfigFileLocation = value;
            }
        }

        public string ConfigVSSLocation
        {
            get
            {
                return (versionNumberConfigVSSLocation == null ? String.Empty : versionNumberConfigVSSLocation);
            }
            set
            {
                versionNumberConfigVSSLocation = value;
            }
        }

        public string VersionNamePath
        {
            get
            {
                return (versionNamePath == null ? String.Empty : versionNamePath);
            }
            set
            {
                versionNamePath = value;
            }
        }

        [Output]
        public string RevisionNumber
        {
            get
            {
                return revisionBuildNumber;
            }
            set
            {
                //revisionBuildNumber = value;
            }
        }
        [Output]
        public int MajorNumber
        {
            get { return majorBuildNumber; }
            set { }
        }

        [Output]
        public int MinorNumber
        {
            get
            {
                return minorBuildNumber;
            }
            set
            {}
        }

        [Output]
        public string BuildNumber
        {
            get
            {
                return this.buildBuildNumber;
            }
            set
            {}
        }

        [Output]
        public string StartDate
        {
            get
            {
                return startDate.ToString(CultureInfo.CurrentCulture);
            }
            set
            {}
        }

        [Output]
        public string VersionNumber
        {
            get
            {
                return (buildNumberFull == null ? String.Empty : buildNumberFull);

            }
            set
            {
                //Setter must be here but don't do anything with the value passed in
            }
        }

        [Output]
        public string VersionName
        {
            get
            {
                return (versionName == null ? String.Empty : versionName);
            }
            set
            {
            }
        }       
    }
}
